namespace Xsxiaosa.Net
{
    using System;
    using System.IO;
//jyj add
	using System.Threading;

    internal abstract class FtpStream : Stream
    {
        // Methods
        public FtpStream(Ftp ftp, bool disconnectOnClose)
        {
            this._closed = false;
            this._finished = false;
            this._exception = null;
            this._ftp = null;
            this._sync = new object();
            this._transferStarted = false;
            this._eventHandler = null;
            this._ftp = ftp;
            this._eventHandler = new FtpTransferProgressEventHandler(this.TransferProgress);
            this._ftp.TransferProgress += this._eventHandler;
            this._disconnectOnClose = disconnectOnClose;
            this._timeout = (this._ftp.Timeout + (this._ftp.AbortTimeout * 2));
        }

        protected void CheckTimeout()
        {
            if (this._timeout <= 0)
            {
                return;
            }
            if ((((long) Environment.TickCount) - this._timerStart) > ((long) this._timeout))
            {
                this.Close();
                throw new FtpException(Xsxiaosa.Net.Messages.GetString(typeof(FtpStrings), "Timeout"), FtpExceptionStatus.Timeout);
            }
        }

        public override void Flush()
        {
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException(Xsxiaosa.Net.Messages.GetString(typeof(FtpStrings), "StreamReadNotSupported"));
        }

        public virtual int ReadInternal(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException(Xsxiaosa.Net.Messages.GetString(typeof(FtpStrings), "StreamReadNotSupported"));
        }

        protected void ResetTimeout()
        {
            this._timerStart = ((long) Environment.TickCount);
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            throw new NotSupportedException(Xsxiaosa.Net.Messages.GetString(typeof(FtpStrings), "StreamSeekNotSupported"));
        }

        public override void SetLength(long length)
        {
            throw new NotSupportedException(Xsxiaosa.Net.Messages.GetString(typeof(FtpStrings), "StreamSeekNotSupported"));
        }

        public void ThrowException()
        {
            if (this._exception == null)
            {
                return;
            }
            if ((this._exception as FtpException) != null)
            {
                throw this._exception;
            }
            throw new FtpException(this._exception.Message, this._exception);
        }

        public void TransferProgress(object sender, FtpTransferProgressEventArgs e)
        {
            if (e.State != FtpTransferState.None)
            {
                this._ftp.TransferProgress -= this._eventHandler;
                this.TransferStarted = true;
            }
        }

        public void WaitForTransfer(FtpTransferState state)
        {
            int num1;
            if (this._timeout <= 0)
            {
                num1 = 0;
            }
            else
            {
                num1 = (Environment.TickCount + this._timeout);
            }
            while (!this.TransferStarted)
            {
                if ((this._timeout >= 0) && (Environment.TickCount > num1))
                {
                    this.Close();
                    throw new FtpException(Xsxiaosa.Net.Messages.GetString(typeof(FtpStrings), "Timeout"), FtpExceptionStatus.Timeout);
                }
                Thread.Sleep(1);
            }
            object obj1 = this._sync;
            lock (obj1)
            {
                if (!this._finished)
                {
                    return;
                }
                this._closed = (state == FtpTransferState.Uploading);
                this.ThrowException();
            }
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException(Xsxiaosa.Net.Messages.GetString(typeof(FtpStrings), "StreamWriteNotSupported"));
        }

        public virtual void WriteInternal(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException(Xsxiaosa.Net.Messages.GetString(typeof(FtpStrings), "StreamWriteNotSupported"));
        }


        // Properties
        public override bool CanRead
        {
            get
            {
                return false;
            }
        }

        public override bool CanSeek
        {
            get
            {
                return false;
            }
        }

        public override bool CanWrite
        {
            get
            {
                return false;
            }
        }

        public override long Length
        {
            get
            {
                throw new NotSupportedException(Xsxiaosa.Net.Messages.GetString(typeof(FtpStrings), "StreamSeekNotSupported"));
            }
        }

        public override long Position
        {
            get
            {
                throw new NotSupportedException(Xsxiaosa.Net.Messages.GetString(typeof(FtpStrings), "StreamSeekNotSupported"));
            }
            set
            {
                throw new NotSupportedException(Xsxiaosa.Net.Messages.GetString(typeof(FtpStrings), "StreamSeekNotSupported"));
            }
        }

        protected bool TransferStarted
        {
            get
            {
//jyj modify                bool flag1;
                object obj1 = this._sync;
                lock (obj1)
                {
                    return this._transferStarted;
                }
//jyj modify                return flag1;
            }
            set
            {
                object obj1 = this._sync;
                lock (obj1)
                {
                    this._transferStarted = value;
                }
            }
        }


        // Fields
        protected bool _closed;
        protected bool _disconnectOnClose;
        private FtpTransferProgressEventHandler _eventHandler;
        protected Exception _exception;
        protected bool _finished;
        protected Ftp _ftp;
        protected object _sync;
        protected int _timeout;
        protected long _timerStart;
        private bool _transferStarted;
    }}

